package com.lancoo.answer.widget.combinationView;

import android.content.Context;
import android.text.Html;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.lancoo.answer.R;
import com.lancoo.answer.model.ConstantBean;
import com.lancoo.answer.model.bean.Child;
import com.lancoo.answer.model.bean.ChildOption;
import com.lancoo.answer.model.bean.Item;
import com.lancoo.answer.model.entity.TaskControlBean;
import com.lancoo.answer.util.DpUtils;
import com.lancoo.answer.util.ExamFontSizeAdjustUtils;
import com.lancoo.answer.util.RichTextUtils;

import java.util.List;

/**
 * Created by pengmi on 2021/12/14 10:58
 * Description:单选题型选项查看
 */
public class OptionLookingView extends RelativeLayout {

//    private static final String TAG = "OptionLookingView";
    private RecyclerView recyclerView;


    public OptionLookingView(Context context) {
        super(context);
        init(context);
    }

    public OptionLookingView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public OptionLookingView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }


    private void init(Context context){
        LayoutInflater.from(context).inflate(R.layout.ev_layout_option_looking,this);
        recyclerView = findViewById(R.id.recycler_view);
        recyclerView.setLayoutManager(new LinearLayoutManager(context));


    }

    public void setChild(Child child,String genreID){
        if(child==null||child.getItemList()==null){
            return;
        }
        Item item = child.getItemList().get(0);
        Adapter mAdapter= new Adapter(child.getChildOptionList(), item,genreID);
        recyclerView.setAdapter(mAdapter);
    }


    public void adjustFontSize(){
        if(recyclerView==null){
            return;
        }
        RecyclerView.Adapter adapter = recyclerView.getAdapter();
        if(adapter==null){
            return;
        }
        adapter.notifyDataSetChanged();
    }


    private static class Adapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{

        private final List<ChildOption> list;
        private final Item item;
        private final String genreID;

        public Adapter(List<ChildOption> list,Item item,String genreID) {
            this.list = list;
            this.item = item;
            this.genreID=genreID;
        }

        @NonNull
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View inflater= LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.ev_layout_item_option_looking,parent,false);
            return new VH(inflater,genreID);
        }

        @Override
        public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
            VH vh= (VH) holder;
            vh.initData(list.get(position),position==list.size()-1);
        }

        @Override
        public int getItemCount() {
            return list.size();
        }


        private  class VH extends RecyclerView.ViewHolder{

            private final String genreID;

            public VH(@NonNull View itemView,String genreID) {
                super(itemView);
                this.genreID=genreID;
                adjustFontSize();
            }

            private void initData(ChildOption childOption,boolean isEnd){
                TextView tv_index=itemView.findViewById(R.id.tv_index);
                TextView tv_content=itemView.findViewById(R.id.tv_content);
                tv_index.setText(childOption.getOptionIndex());
                CharSequence queseAnalysisCharSequence = RichTextUtils.getQueseAnalysisCharSequence(tv_content, Html.fromHtml(childOption.getOption()));
                tv_content.setText(queseAnalysisCharSequence);
                View rl_parent = itemView.findViewById(R.id.rl_parent);
                int dp14= DpUtils.dip2px(itemView.getContext(),14);
                rl_parent.setPadding(0,0,0,isEnd?0:dp14);
                View rl_option = itemView.findViewById(R.id.rl_option);
                View img_state = itemView.findViewById(R.id.img_state);

                tv_index.setVisibility(VISIBLE);
                img_state.setVisibility(INVISIBLE);
                TaskControlBean taskControlBean = ConstantBean.Companion.getTaskControlBean();
                if(TextUtils.isEmpty(item.getStuAnswer())){
                    if(TextUtils.equals(childOption.getOptionIndex(),item.getItemAnswer())){
                        rl_option.setBackgroundResource(R.mipmap.ev_icon_choice_green);
                        tv_index.setTextColor(0xffffffff);
                    }else{
                        rl_option.setBackgroundResource(R.mipmap.ev_icon_choice_white);
                        tv_index.setTextColor(0xff666666);
                    }
                }else{
                    if(TextUtils.equals(childOption.getOptionIndex(),item.getItemAnswer())){
                        rl_option.setBackgroundResource(R.mipmap.ev_icon_choice_green);
                        if(taskControlBean!=null&&taskControlBean.getExpired()) {//过期
                            tv_index.setVisibility(VISIBLE);
                            img_state.setVisibility(INVISIBLE);
                        }else{
                            if(TextUtils.equals(item.getStuAnswer(),item.getItemAnswer())){
                                tv_index.setVisibility(INVISIBLE);
                                img_state.setVisibility(VISIBLE);
                            }
                        }

                        tv_index.setTextColor(0xffffffff);
                    }else{
                        if(TextUtils.equals(item.getStuAnswer(),childOption.getOptionIndex())){
                            if(taskControlBean!=null&&taskControlBean.getExpired()){//过期
                                rl_option.setBackgroundResource(R.mipmap.ev_icon_choice_blue);
                            }else{
                                rl_option.setBackgroundResource(R.mipmap.ev_icon_choice_red);
                            }
                            tv_index.setTextColor(0xffffffff);
                            if(TextUtils.equals(genreID,"14")&&item.getEvalScore()>0){//翻译选择题特殊处理
                                rl_option.setBackgroundResource(R.mipmap.ev_icon_choice_green);
                                tv_index.setVisibility(INVISIBLE);
                                img_state.setVisibility(VISIBLE);
                            }
                        }else{
                            rl_option.setBackgroundResource(R.mipmap.ev_icon_choice_white);
                            tv_index.setTextColor(0xff666666);
                        }
                    }

                }

            }


            public void adjustFontSize(){
                TextView tv_index = itemView.findViewById(R.id.tv_index);
                TextView tv_content = itemView.findViewById(R.id.tv_content);
                ExamFontSizeAdjustUtils.adjustTextViewFontSize(tv_index,15);
                ExamFontSizeAdjustUtils.adjustTextViewFontSize(tv_content,14);
            }
        }

    }
}
